class_name ActorsContainer
extends Node2D


const PREFAB_SHOT := preload("res://scenes/props/shot.tscn")

const PREFAB_SPARK := preload("res://scenes/props/spark.tscn")

const PREFAB_COLLECTIBLE_MAP := {
	Collectible.Type.KNIFE: preload("res://scenes/props/knife.tscn"),
	Collectible.Type.GUN: preload("res://scenes/props/gun.tscn"),
	Collectible.Type.FOOD: preload("res://scenes/props/food.tscn"),
}

const PREFAB_ENEMY_MAP := {
	EnemyData.EnemyType.PUNCK: preload("res://scenes/characters/basic_enemy.tscn"),
	EnemyData.EnemyType.GOON: preload("res://scenes/characters/goon_enemy.tscn"),
	EnemyData.EnemyType.THUG: preload("res://scenes/characters/thug_enemy.tscn"),
	EnemyData.EnemyType.BOUNCER: preload("res://scenes/characters/igor_boss.tscn"),
}


func _init():
	EntityManager.spawn_collectible.connect(on_spawn_collectible)
	EntityManager.spawn_shot.connect(on_spawn_shot)
	EntityManager.spawn_spark.connect(on_spawn_spark)
	EntityManager.orphan_actor.connect(on_orphan_actor)




func on_spawn_collectible(type: Collectible.Type,
						initial_state: String,
						collectible_global_position: Vector2,
						collectible_direction: Vector2,
						initial_height: float,
						auto_destroy: bool):
	if not PREFAB_COLLECTIBLE_MAP.has(type):
		print("Invalid collectible type")
		return
	var collectible: Collectible = PREFAB_COLLECTIBLE_MAP[type].instantiate() as Collectible
	collectible.initial_state = initial_state
	collectible.global_position = collectible_global_position
	collectible.direction = collectible_direction
	collectible.height = initial_height
	collectible.auto_destroy = auto_destroy
	call_deferred("add_child", collectible)


func on_spawn_shot(gun_position: Vector2, distance_traveled: float, height: float):
	var shot := PREFAB_SHOT.instantiate() as Shot
	shot.position = gun_position
	shot.target_distance = distance_traveled
	shot.height = height
	call_deferred("add_child", shot)



func on_spawn_spark(spark_global_position: Vector2):
	var spark := PREFAB_SPARK.instantiate() as Node2D
	spark.global_position = spark_global_position
	call_deferred("add_child", spark)



# 将参数中的节点修改为自己的子节点，会自动挂载未挂载的节点
func on_orphan_actor(node: Node2D):
	if not node.is_node_ready():
		add_child(node)
	node.reparent(self)
